//
//  HPTransactionSuccessViewController.m
//  HPZZTong
//
//  Created by handpay on 14-3-31.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPTransactionSuccessViewController.h"
#import "CategoryUtil.h"
#import "ZZTongDefine.h"

@interface HPTransactionSuccessViewController (){
    
}

@end

@implementation HPTransactionSuccessViewController

@synthesize signButton;
@synthesize bgImageView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)viewDidLoad
{
    self.title = @"交易详情";
    [super viewDidLoad];

    [self.goBackButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    
    [self.goBackButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
    
    UIImage* bgImage = [UIImage imageNamed:@"slipbg.png"];
    
    UIImageView* imageView = [[UIImageView alloc]initWithImage:[bgImage stretchableImageWithLeftCapWidth:0 topCapHeight:192]];
    [self.scrollView addSubview:imageView];
    
    self.bgImageView = imageView;
    [imageView release];
    
    
    if (!iOS7Later) {
        CGRect frame = self.scrollView.frame;
        frame.size.height += frame.origin.y;
        frame.origin.y = 0;
        
        self.scrollView.frame = frame;
    }
    [self setContentData:self.screenData];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [ZZT_UIENG hideNavigationBar:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [ZZT_UIENG hideNavigationBar:YES];
}
- (IBAction)goBackClick:(id)sender {
    [self goBack];
}

- (void)setContentData:(NSDictionary *)dic{
    
    NSDictionary* tempDic = dic;
    if (!dic || [dic count] == 0) {
        tempDic = self.screenData;
    }
    
    if (!tempDic || [tempDic count] == 0) {
        return;
    }

    
    NSArray* belowData = [tempDic objectForKey:kBelowData];
    NSArray* aboveData = [tempDic objectForKey:kAboveData];
    
    CGRect frame = self.scrollView.frame;
    
    int top = 20;
    int h = 30;
    int hSpace = 10;
    
    UILabel* title = [[UILabel alloc]initWithFrame:CGRectMake(0, top, self.scrollView.frame.size.width, h)];
    title.font = kNormalFont;
    title.backgroundColor = [UIColor clearColor];
    title.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:title];
    
    if (belowData) {
        title.textColor = kTitleFontColor;
        title.text = @"支付成功";
        
        UIImageView* successImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"success2.png"]];
        successImageView.frame = CGRectMake(93, top + 5, 20, 20);
        [self.scrollView addSubview:successImageView];
        [successImageView release];
        
        top += h + hSpace;
        
        h = [self createItemData:aboveData haveLine:YES withTop:top];
        top += h + hSpace;
        h = [self createItemData:belowData haveLine:NO withTop:top];
        top += h + hSpace;
        
        UIImageView* failImageV = [[UIImageView alloc]initWithFrame:CGRectMake(100, 160, 157, 157)];
        failImageV.image = [UIImage imageNamed:@"successstamp.png"];
        [self.scrollView addSubview:failImageV];
        [failImageV release];
    }
    [title release];
    top += hSpace;
    if (top > frame.size.height) {
        self.scrollView.contentSize = CGSizeMake(self.scrollView.contentSize.width, top);
    }else{
        self.scrollView.contentSize = frame.size;
    }
    
    CGRect bgImageFrame = self.bgImageView.frame;
    bgImageFrame.size.height = self.scrollView.contentSize.height;
    self.bgImageView.frame = bgImageFrame;
}


- (UIView*)createCornerRadiusView:(CGRect)rect{
    UIView* bgView = [[[UIView alloc]initWithFrame:rect]autorelease];
    [bgView layer].cornerRadius = 8.0;
    [bgView layer].borderColor = kLineFontColor.CGColor;
    [bgView layer].borderWidth = 1;
    bgView.backgroundColor = [UIColor clearColor];
    
    return bgView;
}
- (int)createItemData:(NSArray *)dataArray haveLine:(BOOL)flag withTop:(int)top{
    
    if (!dataArray || [dataArray count] == 0) {
        return top;
    }
    
    int y = 10;
    int x = 5;
    int vX = x + KLABELWIDTH;
    
    CGRect bgFrame = CGRectMake(25, top, self.scrollView.frame.size.width - 50, 20);
    UIView* bgView = [self createCornerRadiusView:bgFrame];
    
    for (int i = 0; i < [dataArray count]; i++) {
        NSArray* arr = [dataArray objectAtIndex:i];
        
        CGRect labelRect = CGRectMake(x, y, KLABELWIDTH, KLINEHEIGHT);
        
        UILabel * label = [[UILabel alloc]initWithFrame:labelRect];
        label.textAlignment = NSTextAlignmentLeft;
        label.text = [arr objectAtIndex:0];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = kNormalFontColor;
        label.font = kNormalFont;
        [bgView addSubview:label];
        [label release];
        
        
        CGRect valueRect = CGRectMake(vX, y, bgFrame.size.width - vX - KLINESPACE, KLINEHEIGHT);
        
        UILabel * labelValue = [[UILabel alloc]initWithFrame:valueRect];
        labelValue.textAlignment = NSTextAlignmentRight;
        labelValue.text = [arr objectAtIndex:1];
        labelValue.textColor = kTitleFontColor;
        labelValue.font = kNormalFont;
        labelValue.backgroundColor = [UIColor clearColor];
        [bgView addSubview:labelValue];
        [labelValue release];
        
        y += KLINEHEIGHT + KLINESPACE;
        
        if (i == 0 && flag) {//画横线
            UIView* lineView = [[UIView alloc]initWithFrame:CGRectMake(10, y, bgFrame.size.width - 20, 1)];
            lineView.backgroundColor = kLineFontColor;
            [bgView addSubview:lineView];
            [lineView release];
            
            y += KLINESPACE + 1;
        }
    }
    y += 10;
    bgFrame.size.height = y;
    bgView.frame = bgFrame;
    [self.scrollView addSubview:bgView];
    
    return  y;
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [_scrollView release];
    self.bgImageView = nil;
    self.signButton = nil;
    [_goBackButton release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setScrollView:nil];
    self.signButton = nil;
    [self setGoBackButton:nil];
    [super viewDidUnload];
}


@end
